package com.bytedance.clockapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.bytedance.clockapplication.widget.Clock;

public class MainActivity extends AppCompatActivity {

    private View mRootView;     // page root
    private Clock mClockView;

    private Handler mHandler = new Handler();   //main thread looper

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRootView = findViewById(R.id.root);        // 绑定页面
        mClockView = findViewById(R.id.clock);      // 绑定时钟控件

        mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取当前时钟模式，设置取反
                mClockView.setShowAnalog(!mClockView.isShowAnalog());

            }
        });

        //start thread
        Thread mthread = new Thread(new Runnable() {
            @Override
            public void run() {
                mHandler.postDelayed(this, 500);    //delay 0.5sec, iteratively
                mClockView.setShowAnalog(mClockView.isShowAnalog());    //reflash the whole view
            }
        });
        mthread.start();    //thread start
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);  //clean up mem to avoid mem leak
    }
}
